package com.inossem.warehousestorage_inossem.adapter.up_frame;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrameItemActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrameOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.UpFrameUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/7/8 0008.
 */

public class UpFrameOverviewAdapter extends BaseAdapter {

    private Context mContext;
    private List<UpFrameOverviewBean.MaterialListBean.ItemListBean> list;
    private UpFrameOverviewBean.MaterialListBean dataBean;
    private LayoutInflater inflater;
    private TextView showAndHide;
    private PercentRelativeLayout hideLayout;
    private TextView warehouseName, materielVoucherNumber, inStorageType, createDate, requestNumber;
    private UpFrameOverviewActivity.UpFrameOverviewListener mUpFrameOverviewListener;
    private String mMaterielCode;
    private EditText matr;
    private ImageView search;


    public UpFrameOverviewAdapter(Context context, UpFrameOverviewBean.MaterialListBean dataBean, List<UpFrameOverviewBean.MaterialListBean.ItemListBean> materielList, String materielCode, UpFrameOverviewActivity.UpFrameOverviewListener upFrameOverviewListener) {
        mContext = context;
        this.dataBean = dataBean;
        this.list = materielList;
        mMaterielCode = materielCode;
        mUpFrameOverviewListener = upFrameOverviewListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size() + 2;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i - 2);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_up_frame_overview_first_item, null);
            //设置数据
            this.warehouseName = (TextView) convertView.findViewById(R.id.warehouseName);
            this.materielVoucherNumber = (TextView) convertView.findViewById(R.id.materielVoucherNumber);
            this.inStorageType = (TextView) convertView.findViewById(R.id.inStorageType);
            this.createDate = (TextView) convertView.findViewById(R.id.createDate);
            this.requestNumber = (TextView) convertView.findViewById(R.id.requestNumber);

            this.warehouseName.setText(dataBean.getWh_name());
            this.materielVoucherNumber.setText(dataBean.getMat_doc_code());
            this.inStorageType.setText(dataBean.getType_name());
            this.createDate.setText(dataBean.getCreate_time());
            this.requestNumber.setText(dataBean.getStock_task_req_code());

            //展开按钮
            hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_up_frame_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_up_frame_overview_show_information));
                }
            });

        } else if (position == 1) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.view_up_frame_overview_second_item, null);
                matr = (EditText) convertView.findViewById(R.id.matr);
                matr.setText(mMaterielCode);
                matr.addTextChangedListener(new QRCodeEditTextWatcher(matr) {
                    @Override
                    public void onScan(String text) {
                        Utils.closeSoftKeyboard(mContext, matr);
                        mUpFrameOverviewListener.onNoDoubleClickListener(text);
                    }
                });
                search = (ImageView) convertView.findViewById(R.id.search);
                search.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        Utils.closeSoftKeyboard(mContext, matr);
                        mUpFrameOverviewListener.onNoDoubleClickListener(matr.getText().toString());
                    }
                });
            }
        } else {
            UpFrameOverviewBean.MaterialListBean.ItemListBean item = (UpFrameOverviewBean.MaterialListBean.ItemListBean) getItem(position);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_up_frame_overview_list, null);
                viewHolder = new UpFrameOverviewAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (UpFrameOverviewAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, item);
        }
        return convertView;
    }

    private void initView(UpFrameOverviewAdapter.ViewHolder viewHolder, View view) {
        viewHolder.unit = (TextView) view.findViewById(R.id.unit);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.serial = (TextView) view.findViewById(R.id.serial);
        viewHolder.notUpFrameNumber = (TextView) view.findViewById(R.id.notUpFrameNumber);
        viewHolder.selectText = (TextView) view.findViewById(R.id.selectText);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.firstRow);
    }

    private void initData(Context context, UpFrameOverviewAdapter.ViewHolder viewHolder, View view, UpFrameOverviewBean.MaterialListBean.ItemListBean item) {
        viewHolder.unit.setText(item.getUnit_zh());
        viewHolder.materielCode.setText(item.getMat_code());
        viewHolder.materielDescription.setText(item.getMat_name());
        viewHolder.serial.setText(item.getStock_task_req_rid());
        viewHolder.notUpFrameNumber.setText(item.getUnstock_task_qty());
        BigDecimal numberBig = UpFrameUtils.getNumber(item.getPositionList());
        if (numberBig.compareTo(BigDecimal.ZERO) > 0) {
            viewHolder.selectText.setText(context.getResources().getString(R.string.view_up_frame_overview_list_UpFrameNumber) + numberBig.toPlainString() + item.getUnit_zh());
        } else {
            viewHolder.selectText.setText(R.string.view_up_frame_overview_list_pleaseInput);
        }

        if (UpFrameUtils.checkMaterielItem(item)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }

        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, UpFrameItemActivity.class);
                intent.putExtra(UpFrameExtra.ITEM_SERIAL, item.getStock_task_req_rid());
                intent.putExtra(UpFrameExtra.LGNUM, dataBean.getWh_id());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView unit, materielCode, materielDescription, serial, notUpFrameNumber, selectText;

        private PercentRelativeLayout headLayout;
    }

}
